Skip to content

Replace big JS dict with JSON parsing - #71250

Merged
bors merged 1 commit into
rust-lang:masterfrom
GuillaumeGomez:use-json-instead-of-js
Apr 20, 2020
Merged

Replace big JS dict with JSON parsing#71250
bors merged 1 commit into
rust-lang:masterfrom
GuillaumeGomez:use-json-instead-of-js

Conversation

@GuillaumeGomez

Copy link
Copy Markdown
Member

Part of #56545.

@ollie27 suggested that using JSON instead of a JS dict might be faster, so I decided to test it. And the results far exceeded whatever expectations I had...

I used https://github.com/adamgreig/stm32ral for my tests. If you want to build it locally:

$ cargo doc --features doc --open

But I strongly recommend to do it with this PR. Some numbers:

  • Loading a page with the JSON search-index: less than 1 second
  • Loading a page with the JS search-index: crashed after 30 seconds

I think the results are clear enough...

r? @ollie27

cc @rust-lang/rustdoc

@jonas-schievink

Copy link
Copy Markdown
Contributor

Nice! This is huge for embedded crates!

Comment thread src/librustdoc/html/render/cache.rs Outdated
@GuillaumeGomez
GuillaumeGomez force-pushed the use-json-instead-of-js branch from d61dd67 to b4fb306 Compare April 17, 2020 16:09
@JohnTitor JohnTitor added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 17, 2020
@kinnison

Copy link
Copy Markdown
Contributor

It would be nice if this could include a statement of the schema of the index, that way rustup might be able to use it to provide rustup doc File for example

@GuillaumeGomez

Copy link
Copy Markdown
Member Author

It's the same as before. But if you the schema anyway, I can send it to you?

@kinnison

Copy link
Copy Markdown
Contributor

Having it formally stated would be good. Parsing JSON in rustup is plausible, where running arbitrary javascript was not :D

@Folyd

ghost commented Apr 17, 2020

Copy link
Copy Markdown
Contributor

Cool!However, is possible to minify the Json search index to reduce the file size?

@GuillaumeGomez

ghost commented Apr 17, 2020

Copy link
Copy Markdown
Member Author

No. That's something I've been working on for a long time and I was able to get some improvements, but nothing extraordinary. The best we can do for the moment is compressing the result (which I expect is done by the users wanting to host their own doc).

@Folyd

ghost commented Apr 18, 2020

Copy link
Copy Markdown
Contributor

Google Chrome team gives an awesome video to explain why the JSON.parse is faster than the native Javascript object literal. Very impressive.
https://www.youtube.com/watch?v=ff4fgQxPaO0

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sad that we're not adding a searchIndex.json file and then loading that in searchIndex.js but I can understand the goal of simplicity.

Would it be possible to have the JSON as a separate file?

Also, doesn't the compiler have serde these days? If so, should we consider filing a help-wanted to convert this into serde code to make it less string-faffy in the future (not for this PR) ?

@kinnison

ghost commented Apr 18, 2020

Copy link
Copy Markdown
Contributor

I'm content with it as-is, though I'd prefer to see the JSON as a separate file if that's not a huge amount of work.

@GuillaumeGomez Have a ponder over my points in the review, and if you feel things are OK enough as-is, or that splitting the JSON into a separate file is too hard for now, then you have r=me.

@kinnison kinnison added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 18, 2020
@Folyd

ghost commented Apr 18, 2020

Copy link
Copy Markdown
Contributor

@kinnison As I know, if we use search-index.json, then we need an extra step to load the JSON file via HTTP request, which brings no efficient, even worse than the current implementation.

@kinnison

ghost commented Apr 18, 2020

Copy link
Copy Markdown
Contributor

That load could be deliberately done as an asynchronous operation, enabling the search box only when the load is complete, and that means the actual usable page load could be faster; but as I said, I'm happy if that doesn't happen in this PR.

@GuillaumeGomez

ghost commented Apr 20, 2020

Copy link
Copy Markdown
Member Author

This is what I tried at first, but you can't load a JSON file locally because of security concerns, meaning that we'd need an HTTP server when using local docs, which I'm strongly opposed to. So unfortunately, we're still forced to keep the JSON as a string in the same file...

Also, the problem here isn't the download of the file but the JS parsing (which is now kinda solved since there is almost no JS).

@GuillaumeGomez

ghost commented Apr 20, 2020

Copy link
Copy Markdown
Member Author

With agreement of @kinnison :

@bors: r=kinnison

@bors

ghost commented Apr 20, 2020

Copy link
Copy Markdown
Collaborator

📌 Commit b4fb306 has been approved by kinnison

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 20, 2020
@bors

ghost commented Apr 20, 2020

Copy link
Copy Markdown
Collaborator

💡 This pull request was already approved, no need to approve it again.

@bors

ghost commented Apr 20, 2020

Copy link
Copy Markdown
Collaborator

📌 Commit b4fb306 has been approved by kinnison

@bors

ghost commented Apr 20, 2020

Copy link
Copy Markdown
Collaborator

⌛ Testing commit b4fb306 with merge aeeae8925dcc1f8a1bb0062efd5d9ba61afd856a...

@Dylan-DPC-zz

ghost commented Apr 20, 2020

Copy link
Copy Markdown

@bors retry (included in rollup)

@bors

ghost commented Apr 20, 2020

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (presumably #71356) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 20, 2020
@bors
bors merged commit 2f06ac0 into rust-lang:master Apr 20, 2020
@GuillaumeGomez
GuillaumeGomez deleted the use-json-instead-of-js branch April 21, 2020 07:58
@alecmocatta

This comment has been minimized.

@Frizi

ghost commented Jun 11, 2020

Copy link
Copy Markdown
Contributor

@alecmocatta JSONP isn't really a solution, as it brings the problem we are trying to avoid in the first place - embedding data in script context.

Still, the search isn't immediately required on load (user likely won't interact with it withing first second anyway), so time to first paint could still be improved by not waiting on it. Possible solution might involve defer or async script attributes.

@alecmocatta

ghost commented Jun 12, 2020

Copy link
Copy Markdown
Contributor

@Frizi Sorry, I was misinterpreting a comment upthread. I was meaning storing the search index as a file like:

callback(JSON.parse("{...}"))

which it turns out is exactly what this PR is doing!

@GuillaumeGomez

ghost commented Jun 19, 2020

Copy link
Copy Markdown
Member Author

@Frizi If you load a page with search query in the URL, search will kick in pretty early (but I'm nitpicking here :p).

@alecmocatta: Yes absolutely. Glad to see we're on the same page!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.